var STScrollComponentInfo = function()
{
	this.hHead;
	this.hBody;
	this.hTail;
	this.vHead;
	this.vBody;
	this.vTail;
}

var STScrollContainer = function( container, scrollContentOption )
{
	var _container 				= container;
	_container.style.position 	= "relative";
	var _scrollContentOption 	= scrollContentOption;
	var _content;
	
	var _scrollHorizontal = undefined;
	var _scrollVirtical;
	
	var _gestureCapture;
	
	function init()
	{
		// 컨텐츠 검색
		var _len = _container.childNodes.length;
		while( --_len > -1 )
		{
			var _child = _container.childNodes[ _len ];
			if( _child.nodeName.toLowerCase().indexOf( "text" ) < 0 )
			{
				_content = _child;
				_content.style.left = "0px";
				_content.style.top = "0px";
				_content.style.position = "absolute";
				initScroll();
				break;
			}
		}
		
		// 제스쳐 이벤트
		_gestureCapture = new STGestureCapture( _container );
		_gestureCapture.onStart = onDragStart;
		_gestureCapture.onUpdate = onDragUpdate;
		_gestureCapture.onEnd = onDragEnd;
	}
	function initScroll()
	{
		if( _scrollHorizontal == undefined )
		{
			_scrollHorizontal = new ScrollComponent( false, _scrollContentOption.hHead, _scrollContentOption.hBody, _scrollContentOption.hTail );
			_scrollHorizontal.getView().style.left = "0px";
			_scrollHorizontal.getView().style.bottom = "0px";
			_container.appendChild( _scrollHorizontal.getView() );
		}
		if( _scrollVirtical == undefined )
		{
			_scrollVirtical = new ScrollComponent( true, _scrollContentOption.vHead, _scrollContentOption.vBody, _scrollContentOption.vTail );
			_scrollVirtical.getView().style.top = "0px"
			_scrollVirtical.getView().style.right = "0px";
			_container.appendChild( _scrollVirtical.getView() );
		}
		
		updateScrollVisible( false );
	}
	
	// 초기화
	init();
	
	// 스크롤 활성/비활성
	var _horizontalScrollEnabled = true;
	this.setHorizontalScrollEnabled = function( boolValue )
	{
		_horizontalScrollEnabled = boolValue;
	}
	var _virticalScrollEnabled = true;
	this.setVirticalScrollEnabled = function( boolValue )
	{
		_virticalScrollEnabled = boolValue;
	}
	// 컨텐츠 교체
	this.setContent = function( content )
	{
		if( _content != undefined )
		{
			_container.removeChild( _content );
		}
		_content = content;
		_content.style.left = "0px";
		_content.style.top = "0px";
		_content.style.position = "absolute";
		_container.insertBefore( _content, _container.firstChild );
		
		this.setHorizontalScrollEnabled( true );
		this.setVirticalScrollEnabled( true );
		initScroll();
	}
	
	
	
	
	// 드래그
	var _currentLeft;
	var _currentTop;
	var _powX;
	var _powY;
	var _moveInterval;
	function onDragStart( vx, vy )
	{
		_powX = 0;
		_powY = 0;
		
		if( _bounceInterval > 0 ) clearInterval( _bounceInterval );
		_bounceInterval = -1;
		
		if( _moveInterval > -1 ) clearTimeout( _moveInterval );
		_moveInterval = -1;
		
		_utx = parseFloat( _content.style.left );
		_uty = parseFloat( _content.style.top );
	}
	function onDragUpdate( vx, vy )
	{
		if( _horizontalScrollEnabled == false ) vx = 0;
		if( _virticalScrollEnabled == false ) vy = 0;
		
		_currentLeft = parseFloat( _content.style.left );
		_currentTop = parseFloat( _content.style.top );
		
		// 벗어났는지 체크[ 가로 ]
		if( (_currentLeft > 0 && vx > 0 ) || (_currentLeft < -_content.scrollWidth+_container.clientWidth && vx < 0 ))
		{
			vx /= 2;
		}
		// 벗어났는지 체크[ 세로 ]
		if( (_currentTop > 0 && vy > 0 ) || ( _currentTop < -_content.scrollHeight+_container.clientHeight && vy < 0 ))
		{
			vy /= 2;
		}
		
		_content.style.left = (_currentLeft+vx)+"px";
		_content.style.top = (_currentTop+vy)+"px";
		
		
		updateScroll();
		updateScrollVisible( true );
		
		// 
		if( _moveInterval > -1 ) clearTimeout( _moveInterval );
		_moveInterval = setTimeout( moveCheck, 700 );
	}
	
	
	function moveCheck()
	{
		onDragEnd( 0, 0, STGestureCaptureType.NONE );
	}
	
	
	function onDragEnd( vx, vy, type )
	{
		if( _moveInterval > -1 ) clearTimeout( _moveInterval );
		_moveInterval = -1;
		
		if( _horizontalScrollEnabled == false ) vx = 0;
		if( _virticalScrollEnabled == false ) vy = 0;
		
		_powX = vx*1.75;
		_powY = vy*1.75;
		
		_isOutH = false;
		_isOutV = false;
		
		if( _bounceInterval > -1 ) clearTimeout( _bounceInterval );
		_bounceInterval = setInterval( onBounceContent, 33 );
	}
	
	// 트윈
	var _bounceInterval = -1;
	var _isOutH;
	var _isOutV;
	function onBounceContent()
	{
		_currentLeft = parseFloat( _content.style.left );
		_currentTop = parseFloat( _content.style.top );
		
		_isOutH = ( _currentLeft > 0 || _currentLeft < -_content.scrollWidth+_container.clientWidth );
		_isOutV = ( _currentTop > 0 || _currentTop < -_content.scrollHeight+_container.clientHeight )
		
		if( Math.abs( _powX ) > 2 )
		{
			if( _isOutH )
			{
				_powX *= 0.48;
			}else{
				_powX *= 0.82;
			}
			_content.style.left = ( _currentLeft+_powX )+"px";
		}else{
			_powX = 0;
		}
		if( Math.abs( _powY ) > 2 )
		{
			if( _isOutV )
			{
				_powY *= 0.48;
			}else{
				_powY *= 0.82;
			}
			_content.style.top = ( _currentTop+_powY )+"px";
		}else{
			_powY = 0;
		}
		
		
		if( _powX == 0 && _powY == 0 )
		{
			console.log( "onBounceContent clear!" );
			clearInterval( _bounceInterval );
			_bounceInterval = -1;
			// 제자리로
			if( _isOutH || _isOutV )
			{
				_isOutH = false;
				_isOutV = false;
				_bounceInterval = setInterval( onBounceReturn, 33 );
			}else{
				updateScrollVisible( false );
			}
		}else{
			updateScroll();
		}
		
	}
	function onBounceReturn()
	{
		_currentLeft = parseFloat( _content.style.left );
		_currentTop = parseFloat( _content.style.top );
		
		var _targetLeft = _currentLeft;
		var _targetTop = _currentTop;
		
		if( _horizontalScrollEnabled )
		{
			if( isAvailableHorizontal() == false )
			{
				_targetLeft = 0;
			}else if( _currentLeft > 0 )
			{
				_targetLeft = 0;
			}else if( _currentLeft < -_content.scrollWidth+_container.clientWidth )
			{
				_targetLeft = -_content.scrollWidth+_container.clientWidth;
			}
		}
		if( _virticalScrollEnabled )
		{
			if( isAvailableVirtical() == false )
			{
				_targetTop = 0;
			}else if( _currentTop > 0 )
			{
				_targetTop = 0;
			}else if( _currentTop < -_content.scrollHeight+_container.clientHeight )
			{
				_targetTop = -_content.scrollHeight+_container.clientHeight;
			}
		}
		
		var _dx = _targetLeft - _currentLeft;
		var _dy = _targetTop - _currentTop;
		if( Math.sqrt( _dx*_dx+_dy*_dy ) < 1 )
		{
			console.log( "onBounceReturn clear!", _targetLeft, _targetTop );
			_content.style.left = _targetLeft+"px";
			_content.style.top = _targetTop+"px";
			
			clearInterval( _bounceInterval );
			_bounceInterval = -1;
			updateScrollVisible( false );
		}else{
			_content.style.left = ( _currentLeft + _dx*0.18 )+"px";
			_content.style.top = ( _currentTop + _dy*0.18 )+"px";
			
			updateScroll();
		}
	}
	
	function updateScrollVisible( value )
	{
		if( value )
		{
			if( _horizontalScrollEnabled && isAvailableHorizontal() ) _scrollHorizontal.getView().style.visibility = "visible";
			if( _virticalScrollEnabled && isAvailableVirtical() ) _scrollVirtical.getView().style.visibility = "visible";
		}else{
			_scrollHorizontal.getView().style.visibility = "hidden";
			_scrollVirtical.getView().style.visibility = "hidden";
		}
	}
	
	function updateScroll()
	{
		_currentLeft = parseFloat( _content.style.left );
		_currentTop = parseFloat( _content.style.top );
		
		
		// 벗어났는지 체크[ 가로 ]
		if( isAvailableHorizontal() )
		{
			var _hSize = ( _container.clientWidth / _content.scrollWidth ) * _container.clientWidth;
			var _minLeft = -_content.scrollWidth+_container.clientWidth;
			var _maxLeft = 0;
			var _targetLeft = ( _currentLeft-_maxLeft )/( _minLeft-_maxLeft )*(_container.clientWidth-_hSize );
			
			if( _currentLeft > 0 )
			{
				_targetLeft = 0;
				_hSize 		-= _currentLeft;
			}
			if( _currentLeft < -_content.scrollWidth+_container.clientWidth )
			{
				_hSize 		-= (-_content.scrollWidth+_container.clientWidth-_currentLeft);
				_targetLeft = _container.clientWidth - Math.max( _hSize, _scrollHorizontal.getMinSize() );
			}
			_scrollHorizontal.getView().style.left = (_targetLeft)+"px";
			_scrollHorizontal.setSize( _hSize );
		}
		
		
		// 벗어났는지 체크[ 세로 ]
		if( isAvailableVirtical() )
		{
			// 세로
			var _vSize = ( _container.clientHeight / _content.scrollHeight ) * _container.clientHeight;
			var _minTop = -_content.scrollHeight+_container.clientHeight;
			var _maxTop = 0;
			var _targetTop = ( _currentTop-_maxTop )/( _minTop-_maxTop )*(_container.clientHeight-_vSize );
			
			if( _currentTop > 0 )
			{
				_targetTop = 0;
				_vSize 		-= _currentTop;
			}
			if( _currentTop < -_content.scrollHeight+_container.clientHeight )
			{
				_vSize 		-= (-_content.scrollHeight+_container.clientHeight-_currentTop);
				_targetTop = _container.clientHeight - Math.max( _vSize, _scrollVirtical.getMinSize() );
			}
			_scrollVirtical.getView().style.top = (_targetTop)+"px";
			_scrollVirtical.setSize( _vSize );
		}
	}
	
	function isAvailableHorizontal()
	{
		return _container.clientWidth < _content.scrollWidth;
	}
	function isAvailableVirtical()
	{
		return _container.clientHeight < _content.scrollHeight;
	}
}